#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 青城子
# datetime： 2021/8/31 20:43 
# ide： PyCharm

import requests
import execjs


class Steam:
    def __init__(self, password="123456", username="tian@qq.com", exe_function="getPwd", ):
        """
        默认逆向的密码：123456，爬虫调用时根据具体情况传入自己的密码
        默认username，爬虫调用时根据具体情况传入自己的登录账号
        默认steam.js中的执行函数名getPwd,若steam.js中函数名不是getPwd，传入具体函数名即可

        :param password: 默认密码
        :param exe_function: 默认steam.js中执行的函数名
        :param username: 默认登录名
        """
        self.url = "https://store.steampowered.com/login/getrsakey/"
        self.data = {
            "donotcache": "1630411448195",
            "username": username,
        }
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
        }
        self.node = execjs.get()
        self.password = password
        self.exe_function = exe_function

    def get_mod_exp(self):
        """
        获取mod、exp
        :return:
        """
        res_json = requests.post(url=self.url, headers=self.headers, data=self.data).json()
        mod = res_json.get("publickey_mod")
        exp = res_json.get("publickey_exp")
        return mod, exp

    def reverse_engineer(self):
        """
        执行js获取逆向后的pwd
        :return:pwd
        """
        mod, exp = self.get_mod_exp()
        ctx = self.node.compile(open("../static/steam.js", encoding="utf-8").read())
        func_name = "{0}('{1}','{2}','{3}')".format(self.exe_function, self.password, mod, exp)
        pwd = ctx.eval(func_name)
        return pwd


if __name__ == '__main__':
    obj = Steam()
    rest = obj.reverse_engineer()
    print(rest)
